home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / include / scribus-ng / styles / styleset.h < prev   
Encoding:
C/C++ Source or Header  |  2008-09-12  |  3.8 KB  |  195 lines

  1.  
  2.  
  3. #ifndef STYLESET_H
  4. #define STYLESET_H
  5.  
  6. #include <assert.h>
  7. #include "style.h"
  8. //Added by qt3to4:
  9. #include <QList>
  10.  
  11.  
  12. template<class STYLE>
  13. class StyleSet : public StyleContext {
  14. public:
  15.     STYLE& operator[] (int index) { 
  16.         assert(index < styles.count()); 
  17.         return * styles[index]; 
  18.     }
  19.     
  20.     STYLE* getDefault(){ return m_default; }        
  21.     
  22.     const STYLE& get(const QString& name) const { 
  23.         return * dynamic_cast<const STYLE*>(resolve(name)); 
  24.     }
  25.     
  26.     const STYLE& operator[] (int index) const { 
  27.         assert(index < styles.count()); 
  28.         return * styles[index]; 
  29.     }
  30.     
  31.     inline int find(const QString& name) const;
  32.  
  33.     inline const Style* resolve(const QString& name) const;
  34.     
  35.     int count() const {
  36.         return styles.count();
  37.     }
  38.     
  39.     STYLE* append(STYLE* style) { 
  40.         styles.append(style); 
  41.         style->setContext(this); 
  42.         return style; 
  43.     }
  44.     
  45.     inline void remove(int index);
  46.     
  47.     inline void redefine(const StyleSet<STYLE>& defs, bool removeUnused=false);
  48.     
  49.     inline void rename(const QMap<QString,QString>& newNames);
  50.     
  51.     STYLE* create(const STYLE& proto) { 
  52.         return append(new STYLE(proto)); 
  53.     }
  54.     
  55.     void makeDefault(STYLE* def) { 
  56.         m_default = def; 
  57.         if(def) 
  58.             def->setContext(this);
  59.         invalidate();
  60.     }
  61.     
  62.     bool isDefault(const STYLE& style) const {
  63.         return &style == m_default;
  64.     }
  65.     
  66.     
  67.     StyleSet() : styles(), m_context(NULL), m_default(NULL) {}
  68.     
  69.     ~StyleSet() { 
  70.         clear(); 
  71.     }
  72.     
  73.     void clear() { 
  74.         while(styles.count()>0) 
  75.         { 
  76.             delete styles.front(); 
  77.             styles.pop_front(); 
  78.         }
  79.         invalidate();
  80.     }
  81.  
  82.     void setContext(const StyleContext* context) {
  83.         bool reallyNew = m_context != context;
  84.         m_context = context; 
  85.         if (reallyNew)
  86.             invalidate();
  87.     }
  88.     
  89.     const StyleContext* context() const { 
  90.         return m_context; 
  91.     }
  92.     
  93.             
  94. private:
  95.     StyleSet(const StyleSet&)             { assert(false); }
  96.     StyleSet& operator= (const StyleSet&) { assert(false); return *this; }
  97.  
  98.     QList<STYLE*> styles;
  99.     const StyleContext* m_context;
  100.     STYLE* m_default;
  101. };
  102.  
  103. template<class STYLE>
  104. inline void StyleSet<STYLE>::remove(int index)
  105. {
  106.     assert(index>=0 && index < styles.count()); 
  107. //    QList<STYLE*> it = styles.at(index);
  108.     if (styles.at(index) == m_default)
  109.         return;
  110. //    delete (*it);
  111. //    styles.erase(it);
  112.     styles.removeAt(index);
  113. }
  114.  
  115. template<class STYLE>
  116. inline int StyleSet<STYLE>::find(const QString& name) const
  117. {
  118.     for (int i=0; i < styles.count(); ++i)
  119.         if (styles[i]->name() == name)
  120.             return i;
  121.     return -1;
  122. }
  123.  
  124. template<class STYLE>
  125. inline const Style* StyleSet<STYLE>::resolve(const QString& name) const
  126. {
  127.     if (name.isEmpty())
  128.         return m_default;
  129.     for (int i=0; i < styles.count(); ++i)
  130.     {
  131.         if (styles[i]->name() == name)
  132.             return styles[i];
  133.     }
  134.     return m_context ? m_context->resolve(name) : NULL;
  135. }
  136.  
  137. template<class STYLE>
  138. inline void StyleSet<STYLE>::redefine(const StyleSet<STYLE>& defs, bool removeUnused)
  139. {
  140.     for (int i=signed(styles.count())-1; i >= 0; --i) 
  141.     {
  142.         bool found = false;
  143.         for (int j=0; j < defs.count(); ++j)
  144.         {
  145.             if (styles[i]->name() == defs[j].name()) 
  146.             {
  147.                 found = true;
  148.                 (*styles[i]) = defs[j];
  149.                 (*styles[i]).setContext(this);
  150.                 if (defs.m_default == defs.styles[j])
  151.                     makeDefault(styles[i]);
  152.                 break;
  153.             }
  154.         }
  155.         if (!found && removeUnused) 
  156.         {
  157.             if (styles[i] == m_default)
  158.                 makeDefault(NULL);
  159.             remove(i);
  160.         }
  161.     }
  162.     for (int j=0; j < defs.count(); ++j)
  163.     {
  164.         if (find(defs[j].name()) < 0) 
  165.         {
  166.             STYLE* newStyle = create(defs[j]);
  167.             if (defs.m_default == defs.styles[j])
  168.                 makeDefault(newStyle);
  169.         }
  170.     }
  171.     invalidate();
  172. }
  173.  
  174. template<class STYLE>
  175. inline void StyleSet<STYLE>::rename(const QMap<QString,QString>& newNames)
  176. {
  177.     for (int i=0; i < styles.count(); ++i)
  178.     { 
  179.         QMap<QString,QString>::ConstIterator it;
  180.         
  181.         it = newNames.find(styles[i]->name());
  182.         if (it != newNames.end())
  183.             styles[i]->setName(it.value());
  184.     
  185.         it = newNames.find(styles[i]->parent());
  186.         if (it != newNames.end())
  187.             styles[i]->setParent(it.value());
  188.     }
  189.     invalidate();
  190. }
  191.     
  192. #endif
  193.  
  194.  
  195.